// pages/activity/shop/shop.js
import { getCanrouselList, getProdList } from "../../../api/user";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    swiperList: [],
    mallId: "",
    productList: [],
    totalPage: 1,
    showCount: 50,
    currentPage: 1,
    loadMoreText: "加载中...",
    showLoadMore: false,
    canShare: false, // 是否拥有分享功能
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initSwiperList();
    this.initProdList();
  },
  onReachBottom() {
    console.log("btn");
    const { currentPage, totalPage } = this.data;
    const newData = {};
    if (currentPage < totalPage) {
      newData.currentPage = currentPage + 1;

      setTimeout(() => {
        this.getProdList();
      }, 300);
    } else {
      newData.loadMoreText = "没有更多数据了!";
    }
    newData.showLoadMore = true;
    this.setData(newData);
  },
  initSwiperList() {
    getCanrouselList().then((res) => {
      console.log("轮播图列表:", res);
      const newData = {};
      newData.swiperList = res.list || [];
      newData.mallId = res.shopId;
      this.setData(newData);
    });
  },
  initProdList() {
    this.productList = [];
    this.currentPage = 1;
    this.totalPage = 1;
    this.getProdList();
  },
  getProdList() {
    getProdList({
      showCount: this.data.showCount,
      currentPage: this.data.currentPage,
    }).then((res) => {
      console.log(res);
      let list =
        res.data.list.map((item) => {
          let tmpName =
            item.name.length >= 25
              ? `${item.name.slice(0, 25)}......`
              : item.name;
          let tmpPicSmall;
          let tmpRate = item.rate ? `${item.rate * 100}%` : "0%";
          let tmpBenifitMoney =
            item.rate && item.price ? (item.rate * 100 * item.price) / 100 : 0;
          if (item.pic.indexOf(",") > -1) {
            tmpPicSmall = item.pic.split(",")[0];
          } else {
            tmpPicSmall = item.pic;
          }
          return {
            nameDis: tmpName,
            picSmallDis: tmpPicSmall,
            rateDis: tmpRate,
            benifitMoneyDis: tmpBenifitMoney,
            ...item,
          };
        }) || [];
      let mallList =
        res.data.hmProductList.map((item) => {
          let tmpName =
            item.name.length >= 25
              ? `${item.name.slice(0, 25)}......`
              : item.name;
          let tmpPicSmall;
          let tmpRate = item.rate ? `${item.rate * 100}%` : "0%";
          let tmpBenifitMoney =
            item.rate && item.price ? (item.rate * 100 * item.price) / 100 : 0;
          if (item.pic.indexOf(",") > -1) {
            tmpPicSmall = item.pic.split(",")[0];
          } else {
            tmpPicSmall = item.pic;
          }
          return {
            nameDis: tmpName,
            picSmallDis: tmpPicSmall,
            rateDis: tmpRate,
            benifitMoneyDis: tmpBenifitMoney,
            tenantId: res.data.hm_tenantId,
            ...item,
          };
        }) || [];
      const newData = {
        productList: [],
      };
      newData.totalPage = res.data.totalPage || 1;
      newData.productList = newData.productList.concat(list).concat(mallList);
      console.log("商品列表", newData.productList);
      newData.showLoadMore = false;
      this.setData(newData);
    });
  },
  shareProduct(e) {
    const { index } = e.currentTarget.dataset;
    const item = this.data.productList[index];
    console.log(index);
    const userId = wx.getStorageSync("userId");
    if (!userId) {
      wx.showToast({
        title: "请先登录",
        icon: "none",
      });
      return;
    }
    let tmpTenantId = item.tenantId ? item.tenantId : this.data.mallId;
    const path = `/pages/good/good?tenantId=${tmpTenantId}&shareId=${userId}&productId=${item.productId}`;
    console.log(path);
    wx.navigateToMiniProgram({
      appId: "wx7239c7a2397fb19c",
      path: path,
      extraData: {
        // splid: id,
        // userId,
        // feedback:true,
      },
      envVersion: "release", // 打开正式版
      success(res) {
        // 打开成功
      },
      fail: function (err) {},
    });
  },
  toProdDetail(e) {
    const { index } = e.currentTarget.dataset;
    const pObj = this.data.productList[index];
    let tmpTenantId = pObj.tenantId ? pObj.tenantId : this.data.mallId;
    wx.navigateTo({
      url: `/pages/activity/good/good?id=${pObj.productId}&mallId=${tmpTenantId}`,
    });
  },
});
